home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC Format (PL) 2008 December
/
PC_Format_122008.iso
/
Multimedia
/
MediaPortal 0.2.3.0
/
MediaPortal_0.2.3.0_Setup.exe
/
scripts
/
imdb
/
yahoo_it.csscript
< prev
Wrap
Text File
|
2007-10-12
|
17KB
|
523 lines
//css_reference "core.dll";
//css_reference "Databases.dll";
//css_reference "utils.dll";
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Collections;
using System.Web;
using System.Text.RegularExpressions;
using MediaPortal.Util;
class Grabber : MediaPortal.Video.Database.IIMDBScriptGrabber
{
public Grabber()
{
}
void MediaPortal.Video.Database.IIMDBScriptGrabber.FindFilm(string strSearch, int iLimit, ArrayList elements)
{
int iCount = 0;
string strTitle;
try
{
string absoluteUri;
string strURL = "http://it.movies.yahoo.com/dynamic/textsearch.html?p=" + strSearch + "&sp=movie";
string strBody = GetPage(strURL, "utf-8", out absoluteUri);
// Mars Warrior @ 03-sep-2004.
// First try to find an Exact Match. If no exact match found, just look
// for any match and add all those to the list. This narrows it down more easily...
int iStartOfMovieList = strBody.IndexOf("<h2> Film </h2>");
int endOfTitleList = strBody.IndexOf("Notizie </h2>");
if (iStartOfMovieList < 0)
{
return;
}
iStartOfMovieList += "<ol".Length;
int iEndOfMovieList = strBody.IndexOf("</ol>", iStartOfMovieList);
if (iEndOfMovieList < 0)
{
iEndOfMovieList = strBody.Length;
}
if (endOfTitleList < iEndOfMovieList && endOfTitleList > iStartOfMovieList)
{
iEndOfMovieList = endOfTitleList;
}
strBody = strBody.Substring(iStartOfMovieList, iEndOfMovieList - iStartOfMovieList);
while ((true) && (iCount < iLimit))
{
////<A HREF="/Title?0167261">Lord of the Rings: The Two Towers, The (2002)</A>
int iAHREF = strBody.IndexOf("<a href=");
if (iAHREF >= 0)
{
int iEndAHREF = strBody.IndexOf("</a>");
if (iEndAHREF >= 0)
{
iAHREF += "<a href=.".Length;
string strAHRef = strBody.Substring(iAHREF, iEndAHREF - iAHREF);
int iURL = strAHRef.IndexOf(">");
if (iURL > 0)
{
strTitle = "";
strURL = strAHRef.Substring(0, iURL);
//if (strURL[strURL.Length - 1] == '\'')
strURL = strURL.Substring(0, strURL.Length - 1);
iURL++;
int iURLEnd = strAHRef.IndexOf("<", iURL);
if (iURLEnd > 0)
{
strTitle = strAHRef.Substring(iURL, iURLEnd - iURL);
}
else
strTitle = strAHRef.Substring(iURL);
///int onclick = strURL.IndexOf(" onclick");
///if (onclick >= 0)
/// strURL = strURL.Substring(0, onclick - 1);
strURL = String.Format("http://it.movies.yahoo.com{0}", strURL);
HTMLUtil htmlUtil = new HTMLUtil();
htmlUtil.ConvertHTMLToAnsi(strTitle, out strTitle);
int endTagLength = "</a>".Length;
int posNextTag = strBody.IndexOf("<", iEndAHREF + endTagLength);
if (posNextTag > 0)
{
string strSub = strBody.Substring(iEndAHREF + endTagLength, posNextTag - (iEndAHREF + endTagLength));
strTitle += strSub;
}
// to avoid including of
if ((strTitle.IndexOf("\n") < 0) && (strTitle.IndexOf(" ") < 0))
{
MediaPortal.Video.Database.IMDB.IMDBUrl url = new MediaPortal.Video.Database.IMDB.IMDBUrl(strURL, strTitle + " (yahoo_it)", "yahoo_it");
elements.Add(url);
}
iCount++;
}
if (iEndAHREF + 1 >= strBody.Length)
break;
iStartOfMovieList = iEndAHREF + 1;
strBody = strBody.Substring(iEndAHREF + 1);
}
else
{
break;
}
}
else
{
break;
}
}
}
catch (Exception ex)
{
MediaPortal.GUI.Library.Log.Error("exception for yahoo lookup of {0} err:{1} stack:{2}",strSearch, ex.Message, ex.StackTrace);
}
}
bool MediaPortal.Video.Database.IIMDBScriptGrabber.GetDetails(MediaPortal.Video.Database.IMDB.IMDBUrl url, ref MediaPortal.Video.Database.IMDBMovie movieDetails)
{
try
{
int iStart = 0;
int iEnd = 0;
movieDetails.Reset();
// add databaseinfo
// may add an another grabber
movieDetails.Database = "yahoo_it";
string strAbsURL;
string strAbsMovieURL;
string strUrlMovieURL;
string strBody = GetPage(url.URL, "utf-8", out strAbsURL);
strAbsMovieURL = strAbsURL;
if (strBody == null || strBody.Length == 0)
return false;
int iPos = strAbsURL.IndexOf("index-");
if (iPos > 0)
{
iPos += "index-".Length;
movieDetails.IMDBNumber = strAbsURL.Substring(iPos);
int pos = movieDetails.IMDBNumber.IndexOf(".html");
if (pos > 0)
movieDetails.IMDBNumber = movieDetails.IMDBNumber.Substring(0, pos);
}
int imPos = strAbsMovieURL.IndexOf("yahoo.com");
if (imPos > 0)
{
imPos += "yahoo.com".Length;
strUrlMovieURL = strAbsMovieURL.Substring(imPos);
int posm = strUrlMovieURL.IndexOf("index-");
if (posm > 0)
strUrlMovieURL = strUrlMovieURL.Substring(0, posm);
}
url.Title = url.Title.Trim();
// cut of " (imdb)"
iEnd = url.Title.IndexOf("(");
if (iEnd >= 0)
movieDetails.Title = url.Title.Substring(0, iEnd);
else
movieDetails.Title = url.Title;
movieDetails.Title = movieDetails.Title.Trim();
string movieTitle = System.Web.HttpUtility.HtmlEncode(movieDetails.Title);
int iDirectedBy = strBody.IndexOf("Regia");
int iCredits = strBody.IndexOf("Sceneggiatura");
int iGenre = strBody.IndexOf("Genere");
if (iGenre < 0)
iGenre = strBody.IndexOf("Generi");
///int iTagLine = strBody.IndexOf("Tagline:</h5>");
int iPlotOutline = strBody.IndexOf("Trama");
int iPlotSummary = strBody.IndexOf("Trama");
int iPlot = strBody.IndexOf("Trama");
int iImage = strBody.IndexOf( movieDetails.IMDBNumber + ".html\'><img src=\"");
if (iImage >= 0)
{
iImage += (movieDetails.IMDBNumber + ".html\'><img src=\"").Length;
}
int iRating = strBody.IndexOf("numrating");
int iCred = strBody.IndexOf("<dt>Cast</dt>");
///int iTop = strBody.IndexOf("Top 250:");
int iTop = -1;
int iYear = strBody.IndexOf("Data di uscita");
if (iYear >= 0)
{
int iYearEnd = strBody.IndexOf("</dd>", iYear);
iYearEnd -= 4;
string strYear = strBody.Substring(iYearEnd, 4);
movieDetails.Year = System.Int32.Parse(strYear);
}
if (iDirectedBy >= 0)
movieDetails.Director = ParseAHREFIMDB(strBody, iDirectedBy, url.URL).Trim();
if (iCredits >= 0)
movieDetails.WritingCredits = ParseAHREFIMDB(strBody, iCredits, url.URL).Trim();
if (iGenre >= 0)
{
iGenre += "Genere</dt><dd>".Length;
int iGenreEnd = strBody.IndexOf("</dd>", iGenre);
movieDetails.Genre = strBody.Substring(iGenre, iGenreEnd - iGenre);
}
if (iRating >= 0) // and votes
{
iRating += "numrating".Length;
iStart = strBody.IndexOf("alt=\"", iRating);
if (iStart >= 0)
{
iStart += "alt=\"".Length;
iEnd = strBody.IndexOf("stars", iStart);
iEnd -= 1;
// set rating
string strRating = strBody.Substring(iStart, iEnd - iStart);
if (strRating != String.Empty)
strRating = strRating.Replace('.', ',');
try
{
movieDetails.Rating = (float)System.Double.Parse(strRating);
movieDetails.Rating = movieDetails.Rating * 2;
if (movieDetails.Rating > 10.0f)
movieDetails.Rating /= 10.0f;
}
catch (Exception)
{
}
movieDetails.Votes = "0";
///if (movieDetails.Rating != 0.0f)
///{
// now, votes
/// movieDetails.Votes = "0";
/// iStart = strBody.IndexOf("(", iEnd + 2);
/// if (iStart > 0)
/// {
/// iEnd = strBody.IndexOf(" votes</a>)", iStart);
/// if (iEnd > 0)
/// {
/// iStart += "(<a href=\"ratings\">".Length; // skip the parantese and link before votes
/// movieDetails.Votes = strBody.Substring(iStart, iEnd - iStart).Trim();
/// }
/// }
///}
}
}
///if (iTop >= 0) // top rated movie :)
///{
/// iTop += "top 250:".Length + 2; // jump space and #
/// iEnd = strBody.IndexOf("</a>", iTop);
/// string strTop = strBody.Substring(iTop, iEnd - iTop);
/// movieDetails.Top250 = System.Int32.Parse(strTop);
///}
///if (iTagLine >= 0)
///{
/// iTagLine += "Tagline:</h5>".Length;
/// iEnd = strBody.IndexOf("<", iTagLine);
/// movieDetails.TagLine = strBody.Substring(iTagLine, iEnd - iTagLine).Trim();
/// movieDetails.TagLine = MediaPortal.Util.Utils.stripHTMLtags(movieDetails.TagLine);
/// movieDetails.TagLine = HttpUtility.HtmlDecode(movieDetails.TagLine); // Remove HTML entities like ½
///}
if (iPlotOutline < 0)
{
if (iPlotSummary > 0)
{
iPlotSummary += "Plot Summary:</h5>".Length;
iEnd = strBody.IndexOf("<", iPlotSummary);
movieDetails.PlotOutline = strBody.Substring(iPlotSummary, iEnd - iPlotSummary).Trim();
movieDetails.PlotOutline = MediaPortal.Util.Utils.stripHTMLtags(movieDetails.PlotOutline);
movieDetails.PlotOutline = HttpUtility.HtmlDecode(movieDetails.PlotOutline); // remove HTML entities
}
}
else
{
iPlotOutline += "Trama</h3>".Length;
iPlotOutline = strBody.IndexOf("<p>", iPlotOutline);
iPlotOutline += "<p>".Length;
iEnd = strBody.IndexOf("<p>", iPlotOutline);
movieDetails.PlotOutline = strBody.Substring(iPlotOutline, iEnd - iPlotOutline).Trim();
movieDetails.PlotOutline = MediaPortal.Util.Utils.stripHTMLtags(movieDetails.PlotOutline);
movieDetails.PlotOutline = HttpUtility.HtmlDecode(movieDetails.PlotOutline); // remove HTML entities
movieDetails.Plot = movieDetails.PlotOutline.Trim();
movieDetails.Plot = HttpUtility.HtmlDecode(movieDetails.Plot); // remove HTML entities
}
if (iImage >= 0)
{
iEnd = strBody.IndexOf("\"", iImage);
movieDetails.ThumbURL = strBody.Substring(iImage, iEnd - iImage).Trim();
}
//plot (removed)
//cast
int iCredEnd = strBody.IndexOf("</dd>", iCred);
int iEndActor = -1;
iCred += "<dd>".Length;
if (iCred >= 0)
{
string strCast = strBody.Substring(iCred, iCredEnd - iCred);
//movieDetails.Cast += strCast;
while ((true))
{
int iActor = strCast.IndexOf("\'>");
if (iActor >= 0)
{
iEndActor = strCast.IndexOf("</a>");
if (iEndActor >= 0)
{
iActor += "\'>".Length;
string strActor = strCast.Substring(iActor, iEndActor - iActor);
strActor = MediaPortal.Util.Utils.stripHTMLtags(strActor).Trim();
strActor = HttpUtility.HtmlDecode(strActor);
movieDetails.Cast += strActor;
movieDetails.Cast += "\n";
}
}
else
{
break;
}
if (iEndActor + 1 >= strCast.Length)
break;
iCred = iEndActor + 1;
strCast = strCast.Substring(iEndActor + 1);
}
}
int iRunTime = strBody.IndexOf("Durata");
if (iRunTime > 0)
{
iRunTime += "Durata</dt><dd>".Length;
string runtime = "";
runtime = strBody.Substring(iRunTime,2);
try
{
movieDetails.RunTime = Int32.Parse(runtime);
}
catch (Exception) { }
movieDetails.RunTime = movieDetails.RunTime * 60;
iRunTime++;
iRunTime++;
iRunTime++;
runtime = strBody.Substring(iRunTime, 2);
try
{
movieDetails.RunTime = movieDetails.RunTime + Int32.Parse(runtime);
}
catch (Exception) { }
}
int mpaa = strBody.IndexOf("MPAA</a>:</h5>");
if (mpaa > 0)
{
mpaa += "MPAA</a>:</h5>".Length;
int mpaaEnd = strBody.IndexOf("</div>", mpaa);
if (mpaaEnd > 0)
{
movieDetails.MPARating = strBody.Substring(mpaa, mpaaEnd - mpaa);
}
}
return true;
}
catch (Exception ex)
{
MediaPortal.GUI.Library.Log.Error("exception for imdb lookup of {0} err:{1} stack:{2}", url.URL, ex.Message, ex.StackTrace);
}
return false;
}
string MediaPortal.Video.Database.IIMDBScriptGrabber.GetName()
{
return "YAHOOIT grabber ";
}
string MediaPortal.Video.Database.IIMDBScriptGrabber.GetLanguage()
{
return "IT";
}
private string GetPage(string strURL, string strEncode, out string absoluteUri)
{
string strBody = "";
absoluteUri = String.Empty;
Stream ReceiveStream = null;
StreamReader sr = null;
WebResponse result = null;
try
{
// Make the Webrequest
//Log.Info("IMDB: get page:{0}", strURL);
WebRequest req = WebRequest.Create(strURL);
result = req.GetResponse();
ReceiveStream = result.GetResponseStream();
// Encoding: depends on selected page
Encoding encode = System.Text.Encoding.GetEncoding(strEncode);
sr = new StreamReader(ReceiveStream, encode);
strBody = sr.ReadToEnd();
absoluteUri = result.ResponseUri.AbsoluteUri;
}
catch (Exception)
{
//Log.Error("Error retreiving WebPage: {0} Encoding:{1} err:{2} stack:{3}", strURL, strEncode, ex.Message, ex.StackTrace);
}
finally
{
if (sr != null)
{
try
{
sr.Close();
}
catch (Exception)
{
}
}
if (ReceiveStream != null)
{
try
{
ReceiveStream.Close();
}
catch (Exception)
{
}
}
if (result != null)
{
try
{
result.Close();
}
catch (Exception)
{
}
}
}
return strBody;
} // END GetPage()
string ParseAHREFIMDB(string strBody, int iahref, string strURL)
{
int iStart = strBody.IndexOf("<a href=\'", iahref);
if (iStart < 0)
iStart = strBody.IndexOf("<A HREF=\'", iahref);
if (iStart < 0)
return "";
int iEnd = strBody.IndexOf("</a>", iStart);
if (iEnd < 0)
iEnd = strBody.IndexOf("</A>", iStart);
if (iEnd < 0)
return "";
iStart += "<a href=\'".Length;
int iSep = strBody.IndexOf(">", iStart);
string strurl = strBody.Substring(iStart, (iSep - iStart) - 1);
iSep++;
string strTitle = strBody.Substring(iSep, iEnd - iSep);
strTitle = MediaPortal.Util.Utils.stripHTMLtags(strTitle);
HTMLUtil htmlUtil = new HTMLUtil();
htmlUtil.ConvertHTMLToAnsi(strTitle, out strTitle);
strTitle = strTitle.Trim();
return strTitle.Trim();
}
string ParseGenresIMDB(string strBody, int iGenre, string url)
{
string strTmp;
string strTitle = "";
string strHRef = strBody.Substring(iGenre);
int iSlash = strHRef.IndexOf(" / ");
int iEnd = 0;
int iStart = 0;
if (iSlash >= 0)
{
int iRealEnd = strHRef.IndexOf(">more<");
if (iRealEnd < 0)
iRealEnd = strHRef.IndexOf("</div>");
while (iSlash < iRealEnd)
{
iStart = iEnd + 2;
iEnd = iSlash;
int iLen = iEnd - iStart;
if (iLen < 0)
break;
strTmp = strHRef.Substring(iStart, iLen);
strTitle = strTitle + ParseAHREFIMDB(strTmp, 0, "") + " / ";
iSlash = strHRef.IndexOf(" / ", iEnd + 2);
if (iSlash < 0)
iSlash = iRealEnd;
}
}
// last genre
iEnd += 2;
strTmp = strHRef.Substring(iEnd);
strTitle = strTitle + ParseAHREFIMDB(strTmp, 0, "");
HTMLUtil htmlUtil = new HTMLUtil();
htmlUtil.ConvertHTMLToAnsi(strTitle, out strTitle);
return strTitle;
}
}